﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using GroceryPriceBook.Core.Service;
using GroceryPriceBook.Core.ViewModel;
using GroceryPriceBook.Core;

namespace GroceryPriceBook.Controllers
{
	public class ProductAmountUnitController : BaseController
	{
		readonly ProductAmountUnitHandler _handler = new ProductAmountUnitHandler();

		// GET: /ProductAmountUnit/
		public ActionResult Index()
		{
			return View(_handler.ReadAll());
		}

		// GET: /ProductAmountUnit/Details/5
		public ActionResult Details(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var productamountunit = _handler.ReadOne(id.Value);
			if (productamountunit == null)
			{
				return HttpNotFound();
			}
			return View(productamountunit);
		}

		// GET: /ProductAmountUnit/Create
		public ActionResult Create()
		{
			return View();
		}

		// POST: /ProductAmountUnit/Create
		// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Create([Bind(Include = "Id,Name")] ProductAmountUnit productamountunit)
		{
			if (!ModelState.IsValid) return View(productamountunit);

			if (_handler.Create(ref productamountunit) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}

			return View(productamountunit);
		}

		// GET: /ProductAmountUnit/Edit/5
		public ActionResult Edit(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var productamountunit = _handler.ReadOne(id.Value);
			if (productamountunit == null)
			{
				return HttpNotFound();
			}
			return View(productamountunit);
		}

		// POST: /ProductAmountUnit/Edit/5
		// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
		// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
		[HttpPost]
		[ValidateAntiForgeryToken]
		public ActionResult Edit([Bind(Include = "Id,Name")] ProductAmountUnit productamountunit)
		{
			if (!ModelState.IsValid) return View(productamountunit);

			if (_handler.Update(ref productamountunit) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}
			return View(productamountunit);
		}

		// GET: /ProductAmountUnit/Delete/5
		public ActionResult Delete(Guid? id)
		{
			if (id == null)
			{
				return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
			}
			var productamountunit = _handler.ReadOne(id.Value);
			if (productamountunit == null)
			{
				return HttpNotFound();
			}
			return View(productamountunit);
		}

		// POST: /ProductAmountUnit/Delete/5
		[HttpPost, ActionName("Delete")]
		[ValidateAntiForgeryToken]
		public ActionResult DeleteConfirmed(Guid id)
		{
			if (_handler.Delete(id) == ServiceResultEnum.Success)
			{
				return RedirectToAction("Index");
			}
			return View(_handler.ReadOne(id));
		}
	}
}
